﻿#include "nodewidget.h"
#include "common/common.h"
#include "model/datamanager.h"
#include <QToolButton>
#include <QComboBox>
#include <QVBoxLayout>
#include <QListView>

/**
 * @brief NodeWidget::NodeWidget
 * @details 构造节点控件并根据角色初始化界面元素。
 * @param type 节点对应的设备角色。
 * @param parent Qt 父对象。
 */
NodeWidget::NodeWidget(DeviceRole type, QWidget *parent)
    : QWidget(parent)
{
    m_role = type;
    initUI();
}

/**
 * @brief NodeWidget::getButton
 * @details 返回当前节点的按钮指针，供外部自定义行为或样式。
 * @return QToolButton 指针。
 */
QToolButton *NodeWidget::getButton() const
{
    return m_btn;
}

/**
 * @brief NodeWidget::getDeviceList
 * @details 遍历下拉框条目并返回名称列表。
 * @return 包含所有选项的字符串列表。
 */
QStringList NodeWidget::getDeviceList()
{
    QStringList texts;
    for (int i = 0; i < m_comboBox->count(); ++i) {
        texts.append(m_comboBox->itemText(i));
    }
    return texts;
}

/**
 * @brief NodeWidget::setDevice
 * @details 将下拉框设定为指定设备名称。
 * @param device 目标设备名称。
 */
void NodeWidget::setDevice(const QString &device)
{
    m_comboBox->setCurrentText(device);
}
/**
 * @brief NodeWidget::getDevice
 * @details 读取下拉框当前选中的设备名称。
 * @return 当前设备名称。
 */
QString NodeWidget::getDevice()
{
    return m_comboBox->currentText();
}

/**
 * @brief NodeWidget::getDeviceRole
 * @details 返回节点绑定的设备角色，以便外部逻辑判断。
 * @return 设备角色枚举值。
 */
DeviceRole NodeWidget::getDeviceRole()
{
    return m_role;
}

/**
 * @brief NodeWidget::setConnectorIconImage
 * @details 根据是否为衰减器调整连接器图标。
 * @param isAttenuator true 时使用衰减器图标。
 */
void NodeWidget::setConnectorIconImage(bool isAttenuator)
{
    if (m_role >= DeviceRole::Connector_SIG2PA && m_role <= DeviceRole::Connector_Transducers2PM) {
        if (isAttenuator)
            m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/Attenuator.png"));
        else {
            if (m_role == DeviceRole::Connector_SIG2PA || m_role == DeviceRole::Connector_PA2Transducers)
                m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/SwitchUnit_H.png"));
            else
                m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/SwitchUnit_V.png"));
        }
    }
}

void NodeWidget::slot_buttonToggled(bool toggled)
{
    m_comboBox->setEnabled(toggled);
}

/**
 * @brief NodeWidget::initUI
 * @details 完成控件创建、图标设置与布局安排，结合不同角色进行差异化配置。
 */
void NodeWidget::initUI()
{
    m_comboBox = new QComboBox(this);
    m_comboBox->setEnabled(false);
    connect(m_comboBox, &QComboBox::currentTextChanged, this, &NodeWidget::sig_itemChanged);
    m_comboBox->addItem(STR_NO_DEVICE);
    m_comboBox->addItems(DataManager::instance()->getDeviceModelList(m_role));
    auto view = new QListView(m_comboBox);
    view->setStyleSheet("QAbstractItemView::item { min-height: -1px; padding: -1px -1px; }");
    m_comboBox->setView(view);

    m_btn = new QToolButton(this);
    m_btn->setCheckable(true);
    m_btn->setFixedSize(QSize(100, 100));
    connect(m_btn, &QToolButton::toggled, this, &NodeWidget::slot_buttonToggled);
    switch (m_role) {
    case DeviceRole::SignalGenerator:
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/SignalGenerator.png"));
        break;
    case DeviceRole::Amplifier:
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/Amplifier.png"));
        m_btn->setFixedSize(190, 160);
        break;
    case DeviceRole::AntennaRadiates:
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/RadiatesAntenna.png"));
        break;
    case DeviceRole::AntennaReceives:
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/ReceivesAntenna.png"));
        break;
    case DeviceRole::FieldProbe:
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/FieldProbe.png"));
        break;
    case DeviceRole::CurrentInjectionProbeInput:
        m_btn->setFixedSize(QSize(70, 95));
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/CurrentInjectionProbe.png"));
        m_comboBox->hide();
        break;
    case DeviceRole::CurrentInjectionProbeMonitor:
        m_btn->setFixedSize(QSize(70, 95));
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/CurrentInjectionProbe.png"));
        m_comboBox->hide();
        break;
    default:
        break;
    }

    if (m_role == DeviceRole::Receiver || m_role == DeviceRole::SpectrumAnalyzer) {
        m_btn->setFixedSize(100, 61);
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/Receiver.png"));
    }

    if (m_role >= DeviceRole::PowerMeterForward && m_role <= DeviceRole::PowerMeterMonitor) {
        m_btn->setFixedSize(100, 61);
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/PowerMeter.png"));
    }
    if (m_role == DeviceRole::PowerMeterMonitor || m_role == DeviceRole::Receiver || m_role == DeviceRole::SpectrumAnalyzer) {
        m_comboBox->clear();
        m_comboBox->addItem(STR_NO_DEVICE);
        m_comboBox->addItems(DataManager::instance()->getDeviceModelList(DeviceRole::PowerMeterMonitor));
        m_comboBox->addItems(DataManager::instance()->getDeviceModelList(DeviceRole::Receiver));
        m_comboBox->addItems(DataManager::instance()->getDeviceModelList(DeviceRole::SpectrumAnalyzer));
    }

    if (m_role >= DeviceRole::Connector_SIG2PA && m_role <= DeviceRole::Connector_Transducers2PM) {
        m_btn->setFixedSize(50, 50);
        m_btn->setIcon(QIcon(":/blacktheme/icons/devIcons/Attenuator.png"));
    }
    m_btn->setIconSize(m_btn->size());
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setSpacing(5);
    if (m_role == CurrentInjectionProbeMonitor
        || (m_role >= DeviceRole::PowerMeterForward && m_role <= DeviceRole::PowerMeterMonitor)
        || m_role == DeviceRole::Receiver || m_role == DeviceRole::SpectrumAnalyzer) {
        layout->addWidget(m_btn, 0, Qt::AlignCenter);
        layout->addWidget(m_comboBox, 0, Qt::AlignCenter);
    } else {
        layout->addWidget(m_comboBox, 0, Qt::AlignCenter);
        layout->addWidget(m_btn, 0, Qt::AlignCenter);
    }
    m_comboBox->setCurrentIndex(0);
    // 设置布局
    setLayout(layout);
}
